// pages/my/changePhone/index.js
const app = getApp();
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    password: null,
    surePassword: null,
    code: null,
    hidden: true,
    hiddenCode: true,
    count:60,  
    countConst:60, 
    isDisabled:false, // 按钮是否禁用
    title:"发送验证码", // 按钮显示文字
    titleConst:"发送验证码"
  },
  countdown: function(){
    let that = this;
    let count = this.data.count;
    // 当count不为0开始倒计时，当count为0就关闭倒计时
    // 定时器
    var countdown = setInterval(function(){
      if( count == 0 ){
        that.setData({
          title:that.data.titleConst,
          count: that.data.countConst,
          isDisabled:false
        });
        // 取消定时器
        clearInterval(countdown);
      } else {
        that.setData({
          title:count-- + "s后重试",
          isDisabled:true
        });
      }
    }, 1000);
  },
  checkPass() { //检查
    console.log(this.data.password)
    console.log(this.data.surePassword)
    console.log(this.data.code)
    var patt = /^\d{6}$/
    if (this.data.password === null) {
      return 1;
    } else if (this.data.password !== this.data.surePassword) {
      return 2;
    } else if (!patt.test(this.data.code)) {
      return 3;
    }

  },
  //获取用户输入的密码
  userPassword: function(e) {
    this.setData({
      password: e.detail.value
    })
  },
  //获取用户输入的确认密码
  userSurePassword: function(e) {
    this.setData({
      surePassword: e.detail.value
    })
  },
  //获取用户输入的验证码
  loginBtnClick: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  //发送验证码
  sendCheck() {
    that.setData({
      hiddenCode: false
    })
    var curPhone = app.globalData.user.phone
    console.log(typeof curPhone)
    if (app.globalData.userType === "admin") {
      var url = app.globalData.BASE_URL + '/admin/adminUpdatePasswordCode'
    } else if (app.globalData.userType === "student1" || app.globalData.userType === "student2") {
      var url = app.globalData.BASE_URL + '/student/studentUpdatePasswordCodeServlet'
    } else if (app.globalData.userType === 'worker') {
      var url = app.globalData.BASE_URL + '/worker/workerUpdatePasswordCodeServlet'
    }
    wx.request({
      url: url,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        token: app.globalData.token,
        userType: app.globalData.userType,
        phone: curPhone
      },
      success(res) {
        that.setData({
          hiddenCode: true
        })
        console.log(res)
        if (res.data == 1) {
          that.countdown();
          wx.showToast({
            title: '发送成功',
            icon: 'none',
          });
        } else if (res.data == 2 || res.data == 3) {
          wx.showToast({
            title: '发送失败！',
            icon: 'none',
          });
        } else if (res.data == 444) {
          wx.navigateTo({
            url: "/pages/login/index?flag=invalid",
          })
        }
      },
      fail(res) {
        that.setData({
          hiddenCode: true
        })
        wx.showToast({
          title: '请求超时！',
          icon: 'none',
        });
      }
    })
  },
  submit() {
    let code = this.data.code
    console.log(code)
    console.log(this.checkPass())
    if (this.checkPass() === 1) {
      wx.showToast({
        title: '请填写密码',
        icon: 'none',
      });
    } else if (this.checkPass() === 2) {
      wx.showToast({
        title: '两次密码不一致',
        icon: 'none',
      });
    } else if (this.checkPass() === 3) {
      wx.showToast({
        title: '请填写6位数字验证码',
        icon: 'none',
      });
    } else {
      this.setData({
        hidden: false
      })
      if (app.globalData.userType === "admin") {
        var url = app.globalData.BASE_URL + '/admin/adminUpdatePassword'
      } else if (app.globalData.userType === "student1" || app.globalData.userType === "student2") {
        var url = app.globalData.BASE_URL + '/student/studentUpdatePasswordServlet'
      } else if (app.globalData.userType === 'worker') {
        var url = app.globalData.BASE_URL + '/worker/workerUpdatePasswordServlet'
      }
      wx.request({
        url: url,
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: {
          token: app.globalData.token,
          userType: app.globalData.userType,
          phone: app.globalData.user.phone,
          password: this.data.password,
          code: code
        },
        success(res) {
          that.setData({
            hidden: true
          })
          console.log(res)
          if (res.data == 1) {
            wx.showToast({
                title: '修改成功',
                icon: 'success',
                duration: 1500
              }),
              setTimeout(() => {
                wx.navigateTo({
                  url: "/pages/login/index?flag=invalid",
                })
              }, 1500)
          } else if (res.data == 2) {
            wx.showToast({
              title: '修改失败',
              icon: 'none',
            })
          } else if (res.data == 3) {
            wx.showToast({
              title: '验证码错误',
              icon: 'none',
            })
          } else if (res.data == 444) {
            wx.navigateTo({
              url: "/pages/login/index?flag=invalid",
            })
          }
        },
        fail(res) {
          that.setData({
            hidden: true
          })
          wx.showToast({
            title: '请求超时！',
            icon: 'none',
          });
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})